package me.linkcn.ydtxl.web;

import me.linkcn.ydtxl.web.resource.AccountResource;
import me.linkcn.ydtxl.web.resource.AccountsResource;
import me.linkcn.ydtxl.web.resource.LoginResource;
import org.restlet.Component;
import org.restlet.Restlet;
import org.restlet.data.LocalReference;
import org.restlet.data.Protocol;
import org.restlet.resource.Directory;
import org.restlet.routing.Router;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Created by IntelliJ IDEA.
 * User: cmcc
 * Date: 11-8-14
 * Time: 下午10:47
 */
public class Application extends org.restlet.Application {
    private final String serverUri;
    private freemarker.template.Configuration fmc;
    private final String webRootPath;

    public Application() throws IOException {
        Properties properties = getProperties("application.properties");
        this.serverUri = properties.getProperty("server.uri");
        this.webRootPath = properties.getProperty("web.root.path");
        try {
            final File templateDir = new File(webRootPath + "/tmpl");
            this.fmc = new freemarker.template.Configuration();
            this.fmc.setDirectoryForTemplateLoading(templateDir);
        } catch (Exception e) {
            getLogger().severe("Unable to configure FreeMarker.");
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
        // Create a component with an HTTP server connector
        final Component component = new Component();
        component.getServers().add(Protocol.HTTP, 8585);
        component.getClients().add(Protocol.FILE);
        component.getClients().add(Protocol.CLAP);
        component.getClients().add(Protocol.HTTP);
        // Attach the application to the default host and start it
        component.getDefaultHost().attach("/txl", new Application());
        component.start();
    }

    public static Properties getProperties(String propertiesUri)
            throws IOException {
        InputStream is = Application.class.getResourceAsStream(propertiesUri);
        Properties properties = new Properties();
        properties.load(is);
        return properties;
    }

    @Override
    public Restlet createInboundRoot() {
        final Router router = new Router(getContext());
        final Directory imgDirectory = new Directory(getContext(),
                LocalReference.createFileReference(webRootPath + "/resources/images"));
        // Add a route for the image resources
        router.attach("/images", imgDirectory);

        final Directory cssDirectory = new Directory(getContext(),
                LocalReference.createFileReference(webRootPath + "/resources/css"));
        // Add a route for the CSS resources
        router.attach("/css", cssDirectory);

        final Directory jsDirectory = new Directory(getContext(),
                LocalReference.createFileReference(webRootPath + "/resources/js"));
        // Add a route for the JS resources
        router.attach("/js", jsDirectory);

        router.attach("/account", AccountsResource.class);
        router.attach("/login", LoginResource.class);
        router.attach("/{accountName}", AccountResource.class);

        return router;
    }

    public String getServerUri() {
        return this.serverUri;
    }

    public freemarker.template.Configuration getFmc() {
        return this.fmc;
    }

}
